﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace MovingChame.Web.Helpers
{
    public static class SecurityHelper
    {
        public static string GetHash(string value)
        {
            var hasher = MD5.Create();
            byte[] data = hasher.ComputeHash(Encoding.Default.GetBytes(value));
            var builder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                builder.Append(data[i].ToString("x2"));
            }
            return builder.ToString();
        }

        public static string GeneratePassword(int length)
        {
            Random random = new Random();
            var result = new byte[length];

            for (int i = 0; i < length; i++)
            {
                var type = random.Next(2);

                if (type == 0)
                {
                    // Letter
                    var textCase = random.Next(2);
                    var character = random.Next(26);

                    if (textCase == 0)
                    {
                        // Lower
                        result[i] = (byte)(character + 97);
                    }
                    else
                    {
                        // Upper
                        result[i] = (byte)(character + 65);
                    }
                }
                else
                {
                    // Number
                    var number = random.Next(10);
                    result[i] = (byte)(number + 48);
                }
            }

            return Encoding.ASCII.GetString(result);
        }
    }
}